﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MountSilverLibraryRemake
{
    public partial class LimitForm : Form
    {
        ListView saleStatus;
        public LimitForm(ref ListView saleStatus)
        {
            InitializeComponent();
            this.saleStatus = saleStatus;
        }
        private bool IsDouble(string str)
        {
            try
            {
                Double i = Convert.ToDouble(str.Trim()); ;
                return true;
            }
            catch
            {
                return false;
            }
        }
        private void btnUSearch_Click(object sender, EventArgs e)
        {
            int selected = key.SelectedIndex;
            for (int i = 0; i < saleStatus.Items.Count;)
            {
                if (selected != 2)
                {
                    if (!saleStatus.Items[i].SubItems[selected].Text.Contains(limitation.Text))
                    {
                        saleStatus.Items.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
                else
                {
                    if (!saleStatus.Items[i].SubItems[5].Text.Contains(limitation.Text))
                    {
                        saleStatus.Items.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
            }
            limitation.Clear();
        }

        private void btnTSearch_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < saleStatus.Items.Count;)
            {
                DateTime date = Convert.ToDateTime(saleStatus.Items[i].SubItems[4].Text);
                DateTime mindate = datemin.Value;
                DateTime maxdate = datemax.Value;
                if (date>maxdate||date<mindate)
                {
                    saleStatus.Items.RemoveAt(i);
                }
                else
                {
                    i++;
                }
            }
            datemin.Value = System.DateTime.Now;
            datemax.Value = datemin.Value;
        }

        private void btnPSearch_Click(object sender, EventArgs e)
        {
            int selected = keydata.SelectedIndex;
            int sign = -1;
            if (IsDouble(min.Text.Trim()) && IsDouble(max.Text.Trim()))
            {
                sign = 2;
            }
            else if (IsDouble(min.Text.Trim()))
            {
                sign = 1;
            }
            else if (IsDouble(max.Text.Trim()))
            {
                sign = 0;
            }
            if (selected == 0)
            {
                if (sign == 0)
                {
                    for (int i = 0; i < saleStatus.Items.Count;)
                    {
                        Double data = Convert.ToDouble(saleStatus.Items[i].SubItems[2].Text);
                        Double maxData = Convert.ToDouble(max.Text.Trim());
                        if (data > maxData)
                        {
                            saleStatus.Items.RemoveAt(i);
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
                else if (sign == 1)
                {
                    for (int i = 0; i < saleStatus.Items.Count;)
                    {
                        Double data = Convert.ToDouble(saleStatus.Items[i].SubItems[2].Text);
                        Double minData = Convert.ToDouble(min.Text.Trim());
                        if (data < minData)
                        {
                            saleStatus.Items.RemoveAt(i);
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
                else if (sign == 2)
                {
                    for (int i = 0; i < saleStatus.Items.Count;)
                    {
                        Double data = Convert.ToDouble(saleStatus.Items[i].SubItems[2].Text);
                        Double maxData = Convert.ToDouble(max.Text.Trim());
                        Double minData = Convert.ToDouble(min.Text.Trim());
                        if (data > maxData || data < minData)
                        {
                            saleStatus.Items.RemoveAt(i);
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
            }
            else
            {
                if (sign == 0)
                {
                    for (int i = 0; i < saleStatus.Items.Count;)
                    {
                        Double data = Convert.ToDouble(saleStatus.Items[i].SubItems[3].Text);
                        Double maxData = Convert.ToDouble(max.Text.Trim());
                        if (data > maxData)
                        {
                            saleStatus.Items.RemoveAt(i);
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
                else if (sign == 1)
                {
                    for (int i = 0; i < saleStatus.Items.Count;)
                    {
                        Double data = Convert.ToDouble(saleStatus.Items[i].SubItems[3].Text);
                        Double minData = Convert.ToDouble(min.Text.Trim());
                        if (data < minData)
                        {
                            saleStatus.Items.RemoveAt(i);
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
                else if (sign == 2)
                {
                    for (int i = 0; i < saleStatus.Items.Count;)
                    {
                        Double data = Convert.ToDouble(saleStatus.Items[i].SubItems[3].Text);
                        Double maxData = Convert.ToDouble(max.Text.Trim());
                        Double minData = Convert.ToDouble(min.Text.Trim());
                        if (data > maxData || data < minData)
                        {
                            saleStatus.Items.RemoveAt(i);
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
            }
            min.Clear();
            max.Clear();
        }
    }
}
